package org.yn.car.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yn.auth.annotation.PermissionAnnotation;
import org.yn.base.util.AjaxResult;
import org.yn.base.util.LoginContext;
import org.yn.base.util.PageInfo;
import org.yn.car.domain.Car;
import org.yn.car.query.CarQuery;
import org.yn.car.service.ICarService;
import org.yn.emp.domain.Employee;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@PermissionAnnotation(name="car管理",descs = "car管理")
@Api("car管理接口")
@RestController
@RequestMapping("/car")
public class CarController {
    @Autowired
    public ICarService carService;


    /**
     * 保存和修改公用的
     * @param car 传递的实体
     * @return Ajaxresult转换结果
     */
    @PermissionAnnotation(name = "保存和修改Car", descs = "保存和修改Car")
    @ApiOperation(value = "保存和修改")
    @PostMapping
    public AjaxResult updateOrInsert(@RequestBody Car car, HttpServletRequest request) {
        Employee loginAdmin = LoginContext.getLoginAdmin(request);
        if ( car.getId() != null)
            carService.update(car);
            else
            carService.insert(car,loginAdmin);
        return AjaxResult.success();
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @PermissionAnnotation(name = "删除Car", descs = "删除Car")
    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
            carService.delete(id);
        return AjaxResult.success();
    }

    //获取用户
    @PermissionAnnotation(name = "获取单个Car", descs = "获取单个Car")
    @ApiOperation(value = "获取单个Car")
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id") Long id) {
        Car car =carService.selectById(id);
        return AjaxResult.success(car);
    }


    /**
    * 查看所有的XX信息
    * @return
    */
    @PermissionAnnotation(name = "获取所有Car", descs = "获取所有Car")
    @ApiOperation(value = "获取所有Car")
    @GetMapping
    public AjaxResult selectAll() {

        List< Car> list = carService.selectAll();
        return AjaxResult.success(list);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageInfo 分页对象
    */
    @PermissionAnnotation(name = "分页查询Car", descs = "分页查询Car")
    @ApiOperation(value = "分页查询Car")
    @PutMapping
    public AjaxResult queryPage(@RequestBody CarQuery query) {
        PageInfo<Car> pageInfo = carService.queryPage(query);
        return AjaxResult.success(pageInfo);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PermissionAnnotation(name = "批量删除Car", descs = "批量删除Car")
    @ApiOperation(value = "批量删除Car")
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
            carService.batchDelete(ids);
        return AjaxResult.success();
    }


    /**
     * 上架
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids,HttpServletRequest request){
        Employee loginUser = LoginContext.getLoginAdmin(request);
        String msg = carService.onsale(ids, loginUser);
        return AjaxResult.success(msg);
    }

    /**
     * 下架
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids,HttpServletRequest request){
        Employee loginUser = LoginContext.getLoginUser(request);
        carService.offsale(ids,loginUser);
        return AjaxResult.success();
    }

    /**
     * 上架单个对象信息
     * @param id
     * @return
     */
    @PermissionAnnotation(name = "上架单个对象信息", descs = "上架单个对象信息")
    @ApiOperation(value = "上架单个对象信息")
    @GetMapping(value = "/onsaleOne/{id}")
    public AjaxResult onsaleOne(@PathVariable("id") Long id,HttpServletRequest request) {
        Employee loginUser = LoginContext.getLoginAdmin(request);
        List<Long> ids = new ArrayList<>();
        ids.add(id);
        String msg = carService.onsale(ids, loginUser);
        return AjaxResult.success(msg);
    }

    /**
     * 下架单个对象信息
     * @param id
     * @return
     */
    @PermissionAnnotation(name = "下架单个对象信息", descs = "下架单个对象信息")
    @ApiOperation(value = "下架单个对象信息")
    @GetMapping(value = "/offsaleOne/{id}")
    public AjaxResult offsaleOne(@PathVariable("id") Long id,HttpServletRequest request) {
        Employee loginUser = LoginContext.getLoginUser(request);
        List<Long> ids = new ArrayList<>();
        ids.add(id);
        carService.offsale(ids,loginUser);
        return AjaxResult.success();
    }

}